﻿using EmailClient.Model;
using EmailClient.ViewModel;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace EmailClient.View
{
    /// <summary>
    /// Interaction logic for SettingsView.xaml
    /// </summary>
    public partial class SettingsView : MetroWindow
    {
        public SettingsView()
        {
            InitializeComponent();
        }

        private void AddAccountPress(object sender, RoutedEventArgs e)
        {
            var form = new EmailAccountView();
            form.mainWindow = this;
            form.Show();
        }

        private void EditAccountPress(object sender, RoutedEventArgs e)
        {
            if (ucAccountsList.dgAccounts.CurrentItem != null)
            {
                var currItem = (EmailAccount)ucAccountsList.dgAccounts.CurrentItem;
                var EditForm = new EmailAccountView(currItem);
                EditForm.mainWindow = this;
                EditForm.Show();
            }        
        }

        private async void DelAccountPress(object sender, RoutedEventArgs e)
        {
            if (ucAccountsList.dgAccounts.CurrentItem != null)
            {
                var currItem = (EmailAccount)ucAccountsList.dgAccounts.CurrentItem;
                await AccountViewModel.Delete(currItem);                
                FillList();
                var mainWindow = Application.Current.MainWindow as MainWindow;
                await mainWindow.UpdateTree();
                await mainWindow.UpdateGrid();
            }    
        }

        private void Accounts_MouseDoubleClick(object sender, RoutedEventArgs e)
        {
            if (ucAccountsList.dgAccounts.CurrentItem != null)
            {
                var currItem = (EmailAccount)ucAccountsList.dgAccounts.CurrentItem;
                var EditForm = new EmailAccountView(currItem);
                EditForm.mainWindow = this;
                EditForm.Show();
            }
        }

        public async void FillList()
        {
            await ucAccountsList.FillList();
        }
    }
}
